package sfeir.ergosum.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseConnection extends SQLiteOpenHelper {
    public DatabaseConnection(Context context) {
        super(context, DatabaseProvider.DATABASE_NAME, null, DatabaseProvider.DATABASE_VERSION);
    }

    // Called when no database exists in
    // disk and the helper class needs
    // to create a new one.
    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_PERSON);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_CONTACT);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_CONTACT_INDEX);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_CONTACT_INDEX2);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_GROUP);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_TEL);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_EMAIL);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_ADDRESS);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_NETWORK);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_LETTERINDEX);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_PHONEINDEX);
        _db.execSQL(DatabaseProvider.DATABASE_CREATE_MAILINDEX);
    }

    // Called when there is a database version mismatch meaning that
    // the version of the database on disk needs to be upgraded to
    // the current version.
    @Override
    public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
            int _newVersion) {
        // Upgrade the existing database to conform to the new version.
        // Multiple previous versions can be handled by comparing
        // _oldVersion and _newVersion values.
        // The simplest case is to drop the old table and create a
        // new one.
        _db.execSQL("DROP TABLE IF EXISTS " + DatabaseProvider.DATABASE_TABLE_NETWORK);
        _db.execSQL("DROP TABLE IF EXISTS " + DatabaseProvider.DATABASE_TABLE_ADDRESS);
        _db.execSQL("DROP TABLE IF EXISTS " + DatabaseProvider.DATABASE_TABLE_EMAIL);
        _db.execSQL("DROP TABLE IF EXISTS " + DatabaseProvider.DATABASE_TABLE_TEL);
        _db.execSQL("DROP TABLE IF EXISTS " + DatabaseProvider.DATABASE_TABLE_CONTACT);
        _db.execSQL("DROP TABLE IF EXISTS " + DatabaseProvider.DATABASE_TABLE_GROUP);
        _db.execSQL("DROP TABLE IF EXISTS " + DatabaseProvider.DATABASE_TABLE_PERSON);
        _db.execSQL("DROP TABLE IF EXISTS " + DatabaseProvider.DATABASE_TABLE_LETTERINDEX);
        _db.execSQL("DROP TABLE IF EXISTS " + DatabaseProvider.DATABASE_TABLE_PHONEINDEX);
        _db.execSQL("DROP TABLE IF EXISTS " + DatabaseProvider.DATABASE_TABLE_MAILINDEX);
        // Create a new one.
        onCreate(_db);
    }

}